package de.tudarmstadt.fgbs.cafe.exception;

import de.tudarmstadt.fgbs.cafe.structure.enumeration.PluginErrorCode;

/**
 * This Exception is supposed to be thrown whenever an error occurs in a plugin.
 * Such as problems with permissions, network or similar.
 * 
 * @author Christian Schlehuber
 * 
 */
public class PluginErrorException extends Exception {

    /**
     * The serial version UID
     */
    private static final long serialVersionUID = -5472583869893176802L;

    /**
     * The error code of the Exception.
     */
    private PluginErrorCode errorCode;

    /**
     * The error message detailing the error.
     */
    private String errorMessage;

    /**
     * Gets the Exception's error code
     * 
     * @return the Exception's error code
     */
    public PluginErrorCode getErrorCode() {
	return errorCode;
    }

    /**
     * Constructs a new {@link PluginErrorException} with the given error code
     * 
     * @param errorCode
     *            the Exception's error code
     * @param errorMessage
     *            the Exception's a message detailing the error's cause
     */
    public PluginErrorException(PluginErrorCode errorCode, String errorMessage) {
	this.errorCode = errorCode;
	this.errorMessage = errorMessage;
    }

    /**
     * 
     * Constructs a new {@link PluginErrorException} with the given error code
     * and cause.
     * 
     * @param errorCode
     *            the Exception's error code
     * @param errorMessage
     *            the Exception's a message detailing the error's cause
     * @param cause
     *            a {@link Throwable} that caused this exception to be thrown
     */
    public PluginErrorException(PluginErrorCode errorCode, String errorMessage,
	    Throwable cause) {
	super(cause);
	this.errorCode = errorCode;
	this.errorMessage = errorMessage;
    }

    /**
     * Gets the Exception's message.
     */
    @Override
    public String getMessage() {
	switch (errorCode) {
	case BAD_CONFIG:
	    return "The Configuration is flawed.";
	case BAD_IMPLEMENTATION:
	    return "The Plugin Implementation is incorrect.";
	case BAD_KEY:
	    return "The key used to access the platform is incorrect or blocked.";
	case INTERNAL_ERROR:
	    return "An internal Error occurred.";
	case NOT_FOUND:
	    return "The ressource was not found.";
	case NOT_IMPLEMENTED:
	    return "The invoked functionality is not implemented.";
	case PERMISSION_DENIED:
	    return "Permission to access the ressource was denied.";
	case PLUGIN_RESTRICTION:
	    return "The call could not be processed due to restrictions of the plugin.";
	case SERVICE_UNAVAILABLE:
	    return "The service is currently unavailable.";
	case CONNECTION_LOST:
	    return "The connection to the plugin has been lost.";
	case UNKNOWN:
	    return "The reason for this error is unknown.";
	default:
	    return null;
	}
    }

    /**
     * Gets the Exception's error message
     * 
     * @return the Exception's error message
     */
    public String getErrorMessage() {
	return errorMessage;
    }
}
